/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-11-22
 * Time: 03:20
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Collections.Generic;

namespace tools
{
	/// <summary>
	/// Reads text based documents into a string-List, line by line.
	/// </summary>
	public class ListFileTool
	{
		#region constructors
		
		/// <summary>
		/// Constructor of ListToFileTool, does nothing.
		/// </summary>
		public ListFileTool()
		{
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Reads the lines in the file to the given list, and returns true if the
		/// operation was successful. Appends or overwrites the list depending on usage
		/// </summary>
		/// <param name="fileName">The name of the file to read</param>
		/// <param name="target">The target list to append/overwrite the lines from the file to (sent by ref)</param>
		/// <param name="append">The read mode, if true, the list is expanded with the content of the file, if false, the list is overwritten with the content of the file</param>
		/// <returns>true if the file content could be read</returns>
		public ToolsResult LoadFileToList(string fileName, List<string> target, bool append)
		{
			ToolsResult result = new ToolsResult();
			
			try
			{
				if(File.Exists(fileName))
				{
					if(!append)
					{
						target.Clear();
					}
					
					StreamReader sr = File.OpenText(fileName);
					string s = sr.ReadLine();
					
					while(s != null)
					{
						if(!s.Equals(""))
						{
							target.Add(s);
						}
						
						s = sr.ReadLine();
					}
					
					sr.Close();
				}
				else
				{
					result.m_Failed = true;
					result.m_Header = "ListFileTool Error, file name '" + fileName + "':";
					result.m_Result = "File not found.";
				}
			}
			catch(Exception e)
			{
				result.m_Failed = true;
				result.m_Header = "ListFileTool Error, file name '" + fileName + "':";
				result.m_Result = e.Message;
			}
			
			return result;
		}
		
		/// <summary>
		/// Prints a List with string in it to the given filename, either in append mode,
		/// or in overwrite mode.
		/// </summary>
		/// <param name="fileName">The name of the file to write to</param>
		/// <param name="list">The list to write, each element will be printed to a separate line</param>
		/// <param name="overWrite">If this is set to true, the file will be overwritten (if existing),
		/// if false, the list will be appended to the file</param>
		/// <returns>true if the save operation was carried out</returns>
		public ToolsResult SaveListToFile(string fileName, List<string> list, bool overWrite)
		{
			ToolsResult result = new ToolsResult();
			
			try
			{
				if(!overWrite)
				{
					if(File.Exists(fileName))
					{
						result.m_Failed = true;
						result.m_Header = "ListFileTool Error, file name '" + fileName + "':";
						result.m_Result = "File already existed, and overwrite was forbidden. List not saved.";
					}
				}
				
				if(!result.m_Failed)
				{
					if(File.Exists(fileName))
					{
						try
						{
							File.Delete(fileName);
						}
						catch(Exception e)
						{
							result.m_Failed = true;
							result.m_Header = "ListFileTool Error, file name '" + fileName + "':";
							result.m_Result = "While attempting to overwrite, exception was raised:\n" + e.Message;
						}
					}
					
					StreamWriter sw = File.CreateText(fileName);
					
					for(int i = 0; i < list.Count; i++)
					{
						sw.WriteLine(list[i]);
					}
					
					sw.Close();
				}
			}
			catch(Exception e)
			{
				result.m_Failed = true;
				result.m_Header = "ListFileTool Error, file name '" + fileName + "':";
				result.m_Result = "Fatal error while attempting to save to the data file, exception was raised:\n" + e.Message;
			}
			
			return result;
		}
		
		/// <summary>
		/// Determins the directory of a file. If the passed string containsa full path, that path will just be returned. If
		/// the passed string only holds a file name, the current path will be returned. If this method is called early in a
		/// program, before any file operations are done, it's possible to store the path to the directory from which the
		/// application was run.
		/// </summary>
		/// <param name="fileName">The name of the file to determine the path to</param>
		/// <returns>The path to the given file name as a string</returns>
		public string GetDirectoryOfFile(string fileName)
		{
			string name = "";
			
			try
			{
				if(!File.Exists(fileName))
				{
					StreamWriter sw = File.CreateText(fileName);
					sw.Flush();
					sw.Close();
				}
				
				FileStream fs = File.OpenRead(fileName);
				name = fs.Name;
				fs.Close();
			}
			catch
			{
				name = "ListFileTool Error: Fatal, unknown, error while determining the starting directory of the application..";
			}
			
			return name;
		}
		
		#endregion
	}
}
